package com.bz.sunmipay.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.baizhi.baseapp.activity.BaseActivity;
import com.baizhi.baseapp.widget.WToast;
import com.bz.sunmipay.R;
import com.bz.sunmipay.controller.GlobalDataHolder;
import com.bz.sunmipay.database.DBHelper;
import com.bz.sunmipay.database.PaymentDetailDatabase;
import com.bz.sunmipay.model.PaymentDetail;
import com.bz.sunmipay.utils.AppUtils;
import com.bz.sunmipay.utils.ArithmeticUtils;
import com.bz.sunmipay.utils.IntentUtils;

/**
 * charge
 */
public class PaymentChargeActivity extends BaseActivity implements View.OnClickListener {
    private PaymentDetailDatabase mPaymentDetailDatabase;
    private PaymentDetail mPaymentDetailData;

    TextView charge_tv_vat;
    TextView charge_et_vat;
    TextView charge_tv_total_value;
    EditText charge_tv_given_value;
    TextView charge_tv_change_value;
    String mTotal;
    String mVat;
//    int pageFrom;
    Button payment_btn_charge;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_payment_charge);
        setLeftContentIcon(R.mipmap.back);
        setRightContentIcon(R.mipmap.home);
        setCenterText("PAYMENT");
        getExtra();
        initContentView();

        mPaymentDetailDatabase = new PaymentDetailDatabase(this);
    }

    private void getExtra() {
        mTotal = getIntent().getStringExtra("amount");
        mVat = getIntent().getStringExtra("vat");
//        pageFrom = getIntent().getIntExtra("pageFrom", 0);
    }

    private void databaseParams() {
        mPaymentDetailData = new PaymentDetail();
        mPaymentDetailData.amount = charge_tv_change_value.getText().toString();
        mPaymentDetailData.userId = GlobalDataHolder.getInstance(this).getUid();
        mPaymentDetailData.userName = GlobalDataHolder.getInstance(this).getUserInfo().getNickname();
        mPaymentDetailData.paymentResult = "1";
        mPaymentDetailData.paymentType = "-1";
        addDatabase(mPaymentDetailData);
    }

    private void addDatabase(PaymentDetail paymentDetail) {
        mPaymentDetailDatabase.insert(paymentDetail, DBHelper.TABLE_NAME);
    }

    private void updateDatabase(PaymentDetail paymentDetail) {
        mPaymentDetailDatabase.update(paymentDetail, DBHelper.TABLE_NAME);
    }

    @Override
    protected void handleClickEvent(int event) {
        super.handleClickEvent(event);
        if (event == RIGHT_BUTTON) {
            IntentUtils.goToHome(this);
        }
    }

    private void initContentView() {
        charge_tv_vat = findViewById(R.id.charge_tv_vat);
        charge_et_vat = findViewById(R.id.charge_et_vat);
        charge_tv_total_value = findViewById(R.id.charge_tv_total_value);
        charge_tv_given_value = findViewById(R.id.charge_tv_given_value);
        charge_tv_change_value = findViewById(R.id.charge_tv_change_value);
        payment_btn_charge = findViewById(R.id.payment_btn_charge);
        charge_tv_total_value.setText(mTotal);
        charge_et_vat.setText(mVat);
        payment_btn_charge.setOnClickListener(this);
        updateChangeValue(charge_tv_given_value.getText().toString().trim());
        charge_tv_given_value.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                updateChangeValue(s.toString().trim());
            }
        });
    }

    /**
     * change = given - total
     * @param givenString
     */
    private void updateChangeValue(String givenString) {
        try {
            final String change = ArithmeticUtils.sub(givenString, mTotal, ArithmeticUtils.DEF_SCALE);
            charge_tv_change_value.setText(change);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.payment_btn_charge:
                charge_tv_given_value.clearFocus();
//                if (pageFrom == IntentUtils.PAGE_FROM_CASH) {
                final String amount = charge_tv_change_value.getText().toString().trim();
                float parseLong = AppUtils.convertToFloat(amount, 0);
                if (parseLong <= 0) {
                    Toast.makeText(this, "Please enter the correct given amount", Toast.LENGTH_SHORT).show();
                    return;
                }
                WToast.showToast("the deal is finished");
                databaseParams();
                IntentUtils.goToHome(this);
//                }
//                else if (pageFrom == IntentUtils.PAGE_FROM_CARD) {
//                    final String amount = charge_tv_change_value.getText().toString().trim();
//                    final String vat = charge_et_vat.getText().toString().trim();
//                    float parseLong = AppUtils.convertToFloat(amount, 0);
//                    if (parseLong <= 0) {
//                        Toast.makeText(this, "Please enter the correct consumption amount", Toast.LENGTH_SHORT).show();
//                        return;
//                    }
//                    IntentUtils.goToPaymentPatternPage(PaymentChargeActivity.this, amount, vat);
//                }
                break;
        }
    }

    private void showSoftKeyboard(View view, Context mContext) {
        if (view.requestFocus()) {
            InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        dismissProgress();
    }

}
